package com.name.givename.controller;

import com.github.pagehelper.Page;
import com.name.givename.dto.NameListDTO;
import com.name.givename.param.ExamineNameParams;
import com.name.givename.param.PageParams;
import com.name.givename.result.ResultVoUtils;
import com.name.givename.service.UserNameInfoService;
import com.name.givename.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examine")
@Api("起名审核")
public class ExamineController {

    @Resource
    private UserNameInfoService userNameInfoService;

    @ApiOperation("待审核名称")
    @GetMapping("/examineName/{pageNumber}/{pageSize}/{order}")
    public ModelAndView examineName(@PathVariable("pageNumber")String pageNumber,@PathVariable("pageSize")String pageSize,@PathVariable("order") String order){

        PageParams pageParams = new PageParams();
        pageParams.setOrderId(Integer.parseInt(order));
        pageParams.setPageNumber(Integer.parseInt(pageNumber));
        pageParams.setPageSize(Integer.parseInt(pageSize));

        Page<NameListDTO> names = userNameInfoService.getUserNames();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("names",names);

        return new ModelAndView("examine",map);
    }

    @ApiOperation("审核名称")
    @PostMapping("/updateNameStatus")
    public Result<Integer> updateNameStatus(@RequestBody ExamineNameParams examineNameParams){
         if (examineNameParams.getStatus().equals("1")){ //  审核通过
             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
             examineNameParams.setUpdate_time(format.format(new Date()));
             userNameInfoService.updateNameStatus(examineNameParams);
             System.out.println("><<<<<<<>>>>>>>>>><><><><><><>><>通过");
         }
        if (examineNameParams.getStatus().equals("2")){ //  审核驳回
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            examineNameParams.setUpdate_time(format.format(new Date()));
            userNameInfoService.updateNameStatus(examineNameParams);
            System.out.println("><<<<<<<>>>>>>>>>><><><><><><>><>驳回");
        }
         // todo 审核未通过
         return ResultVoUtils.success(1);
    }
}

